/*
 * ResultSetUpdatableDemo.java
 *
 * Created on 7 de abril de 2005, 17:47
 */

package jdbc_04;
import jdbc_01.Conexion;

public class ResultSetUpdatableDemo {
    
    Conexion con;
    javax.swing.JTextArea jta;
    
    ResultSetUpdatableDemo() {
        Conexion.driverOdbc();
        con = new Conexion("empleados", "empleados", "empleados");
        if (con.abrirConexion()) {
            verDatos();
            con.cerrarConexion();
        }
    }
    
    void verDatos(){
        jta = new javax.swing.JTextArea(10,40);
        try {
            java.sql.PreparedStatement pstmt = con.getCon().prepareStatement(
                    "Select * from empleados where iddepto = ?",
                    java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, 
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            
            pstmt.setInt( 1, 2 ); 
            
            java.sql.ResultSet rset = pstmt.executeQuery();
            java.sql.ResultSetMetaData rsmd = rset.getMetaData();
            
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                jta.append(rsmd.getColumnName(i).toUpperCase());
                if (rsmd.getColumnName(i).equals("nacimiento")) jta.append("\t");
                jta.append("\t");
            }
            jta.append("\n");

            while (rset.next()) {
                leerDatos(rset, rsmd.getColumnCount());
                rset.updateInt("idproye",  1);
                rset.updateRow();
            }
            
            jta.append("\n");
            while (rset.previous())     
                leerDatos(rset, rsmd.getColumnCount());
            
            rset.close();    
            pstmt.close();
        } catch (java.sql.SQLException e) {
            jta.setText(e.getMessage());
        } finally {
            javax.swing.JOptionPane.showMessageDialog(null, 
                                new javax.swing.JScrollPane(jta));
        }
    }
    
    void leerDatos(java.sql.ResultSet rset, int cols){
        try {
            for ( int i = 1; i <= cols; i++) {
                Object o = rset.getObject(i);
                if ( o != null )
                    jta.append(o.toString().trim());
                else 
                    jta.append("\t");
                jta.append("\t");
            }
            jta.append("\n");

        } catch (java.sql.SQLException e) {
            jta.append("\n" + e.getMessage() + "\n");
        }
    }    
    
    public static void main(String[] args) {
        new ResultSetUpdatableDemo();
        System.exit(0);
    }
    
}
